package com.restaurantManager.shared.utils
{
	import caurina.transitions.properties.DisplayShortcuts;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	
	import mx.controls.Alert;
	import mx.formatters.CurrencyFormatter;
	import mx.resources.ResourceManager;
	
	public class Utils
	{
		public function Utils()
		{
			
		}
		
		private static var formatter:CurrencyFormatter;
		
		private static function initFormatter():void
		{
			formatter = new CurrencyFormatter();
			formatter.alignSymbol="left";
		   	formatter. currencySymbol="$"
		    formatter.precision="0"
		    formatter.rounding="none"
		    formatter.useNegativeSign="true"
		    formatter.useThousandsSeparator="true"
		}
		
		public static function formatValue( val:int ):String
		{
			if ( !formatter ) initFormatter();
			return formatter.format( val );
		}
		
		public static function convertCurrencyToInt( str:String ):int
		{
			if ( !str ) return 0;
			
			str = str.replace( "$", "" );
			str = str.replace( ",", "" );
			str = str.replace( ",", "" );
			str = str.replace( ",", "" );
			str = str.replace( ".", "" );
			str = str.replace( "+", "" );
			//if ( str.search( "-" ) > -1 )
			return ( int( str ) );
				
		}
		public static function isEven( val:int ):Boolean
		{
			return ( val % 2 == 0 );
		}
		
		public static function randomNum( min:Number = 0, max:Number = 1, giveRandomSign:Boolean = false ):Number
		{
			var r:Number = max - Math.random() * ( max - min );
			
			if ( giveRandomSign )
				r *=  (Math.random() > 0.5? -1 : 1); 
			
			return r;
		}
		
		public static function clearContainer( container:DisplayObjectContainer ):void
		{
			if ( container )
			{
				var l:int = container.numChildren;
				for ( var c:int = 0; c < l; c++ )
				{
					container.removeChildAt( 0 );
				}
			}
		}
		
		/**
		 * Centers a displayObject inside its parent object.
		 * @param	object Object to be centered
		 * @param	centerOnX Center horizontally
		 * @param	centerOnY Center vertically
		 */
		public static function centerWithinParent(object:DisplayObject, centerOnX:Boolean = true, centerOnY:Boolean = true ):void 
		{
			//Checks to see if object is valid
			if (!object) return;
			if (!object.parent) return;
			
			if (centerOnX)
				object.x = object.parent.width / 2 - object.width / 2;
			if(centerOnY)
				object.y = object.parent.height / 2 - object.height / 2;
			
		}
		
		/**
		 * Resizes a displayObject to fit inside its parent.  
		 * @param	Object to be resized
		 * @param	borderX Size of x border between object and parent.  0 for none
		 * @param	borderY Size of y border between object and parent.  0 for none
		 */
		public static function resizeByParent(object:DisplayObject, parent:DisplayObject, borderX:Number = 15, borderY:Number = 15):void
		{
			//Checks to see if object is valid
			if (!object) return;
			if (!parent) return;
			var maxX:Number = parent.width - borderX * 2;
			var maxY:Number = parent.height - borderY * 2;

			resizeByMax(object, maxX, maxY);
		}
		
		//Proportionally scales the passed in displayObject so it fits within the passed in constraints
		public static function resizeByMax( object:DisplayObject, maxWidth:Number, maxHeight:Number ):void
		{
			var w:Number = object.width;
			var h:Number = object.height;
			
			var wr:Number =  w / maxWidth;
			var hr:Number =   h / maxHeight;
			
			
			if ( wr > hr )
			{
				object.width = maxWidth;
				object.scaleY = object.scaleX;
								
			}
			else
			{
								
				object.height = maxHeight;
				object.scaleX = object.scaleY;
				
				
			}
			
		}
		
		//Proportionally scales the passed in displayObject so that it's width and height
		//are at least as large as the passed in values
		public static function resizeByMin( object:DisplayObject, minWidth:Number, minHeight:Number ):void
		{
			
			var w:Number = object.width;
			var h:Number = object.height;
			
			var wr:Number =  w / minWidth;
			var hr:Number =   h / minHeight;
			
			
			if ( wr < hr )
			{
				object.width = minWidth;
				object.scaleY = object.scaleX;
				if ( object.height < minHeight - 5 )
				{
					object.height = minHeight;
					object.scaleX = object.scaleY;
				}
				
			}
			else
			{
								
				object.height = minHeight;
				object.scaleX = object.scaleY;
				if ( object.width < minWidth - 5 )
				{
					object.width = minWidth;
					object.scaleY = object.scaleX;
				}
				
			}
			
		}
		
		//Sets up all 'Alert'(s), 'chatMessage'(s), and 'toolTip'(s) for multiple languages.  text inside " ' ' " are actual variable names
		  public static function setupString ( keyString:String, replaceObject:Object = null ):String
		{ 
			var MY_BUNDLE:String = "CardMessages"
			var startString:String = ResourceManager.getInstance().getString(MY_BUNDLE , keyString);
			for ( var name:String in replaceObject  )
			{
				var replaceStringCheck:String = "*" + name + "*";
				startString = startString.replace(replaceStringCheck , replaceObject[name]);
			}
			return (startString);
			
		}
		
		//Creates and returns a randomized version of the array passed in.
		public static function randomizeArray(array:Array):Array
		{
			var _randomArray:Array = new Array();
			var _finalArray:Array = new Array();
			 
			var l:Number = array.length;
			for ( var c:int = 0; c < l; c++ )
			{
				var temp:Object = { index:array[c], sort:Math.random() };
				_randomArray.push( temp );
			}
			_randomArray.sortOn( "sort" );
			
			for ( c = 0; c < l; c++ )
			{
				_finalArray.push(_randomArray[c].index);
			}

			return _finalArray;
		}
	}
}